//
//  WMBaseViewController.swift
//  deWaterMarkingProject
//
//  Created by admin on 2025/9/5.
//

import UIKit

class WMBaseViewController: UIViewController {
    
    var customNavBar: WMCustomNavigationBar!
    var isNavigationBarHidden: Bool = false {
            didSet {
                customNavBar.isHidden = isNavigationBarHidden
                setNeedsStatusBarAppearanceUpdate()
            }
        }
    var statusBarStyleLight: Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCustomNavigationBar()
    }
    
    private func setupCustomNavigationBar() {
        // 隐藏系统导航栏
        navigationController?.setNavigationBarHidden(true, animated: false)
        
        // 创建自定义导航栏
        customNavBar = WMCustomNavigationBar()
        view.addSubview(customNavBar)
        
        // 设置约束
        customNavBar.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            customNavBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            customNavBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            customNavBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            customNavBar.heightAnchor.constraint(equalToConstant: 44)
        ])
        
        // 默认设置为透明样式
        customNavBar.setStyle(.opaque)
        
        // 如果不是根视图控制器，添加返回按钮
        if let navigationController = self.navigationController, navigationController.viewControllers.count > 1 {
            customNavBar.addLeftButton(image: UIImage(named: "Public_Navi_Left_Black"), target: self, action: #selector(backButtonClicked))
        }
    }
    
    @objc func backButtonClicked() {
        navigationController?.popViewController(animated: true)
    }
    
    override var prefersStatusBarHidden: Bool {
          return isNavigationBarHidden
      }
    
    func creatLeftBackButton(_ backIconImageName: String = "Public_Navi_Left_Black") {
        let backBtn = UIButton(type: .custom)
        backBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        backBtn.contentHorizontalAlignment = .left
        backBtn.setImage(UIImage(named: backIconImageName), for: .normal)
        backBtn.addTarget(self, action: #selector(naviBackAct), for: .touchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBtn)
    }
    
    @objc func naviBackAct() {
        self.navigationController!.popViewController(animated: true)
    }
    
    //MARK: - 导航栏右按钮
    func creatRightButton(_ title: String, titleColor: UIColor = UIColor(valueRGB: 0x333333, alpha: 1)) {
        let rightBtn = UIButton(type: .custom)
        rightBtn.frame = CGRect(x: 0, y: 0, width: 56, height: 44)
        rightBtn.contentHorizontalAlignment = .right
        rightBtn.setTitle(title, for: .normal)
        rightBtn.setTitleColor(titleColor, for: .normal)
        rightBtn.titleLabel?.font = UIFont(name: AppFont.regular, size: 15) ?? UIFont.systemFont(ofSize: 15)
        rightBtn.addTarget(self, action: #selector(naviRightBtnAct), for: .touchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
    }
    
    @discardableResult
    func creatRightButton(_ normalImage: UIImage) -> UIButton {
        let rightBtn = UIButton(type: .custom)
        rightBtn.frame = CGRect(x: 0, y: 0, width: 56, height: 44)
        rightBtn.contentHorizontalAlignment = .right
        rightBtn.adjustsImageWhenHighlighted = false
        rightBtn.setImage(normalImage, for: .normal)
        rightBtn.addTarget(self, action: #selector(naviRightBtnAct), for: .touchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
        return rightBtn
    }
    
    @objc func naviRightBtnAct() {
    }
    
    
    func setNavBacColor(isThemeColor: Bool) {
        if(isThemeColor){//导航栏主题色
            
            if (self.navigationController is WMBaseNavigationController) {
                (self.navigationController as! WMBaseNavigationController).setNavigationBar(UIColor(valueRGB:  UInt(OEMInfo.themeHex), alpha: 1.0), UIColor.white)
            }
            self.statusBarStyleLight = false
            if (self.navigationController != nil && (self.navigationController?.viewControllers.count)! > 1) {
                self.creatLeftBackButton("Public_Navi_Left_White")
            }
            
        }else{
            
            if (self.navigationController is WMBaseNavigationController) {
                (self.navigationController as! WMBaseNavigationController).setNavigationBar(UIColor.white, UIColor.black)
            }
            self.statusBarStyleLight = false
        }
    }
    
    func setNavClearColor(){
        
        if #available(iOS 15.0, *) {
            let barApp = UINavigationBarAppearance.init()
            barApp.backgroundColor = UIColor.clear;
            barApp.backgroundEffect = nil;
            barApp.shadowColor = nil;
            self.navigationController?.navigationBar.scrollEdgeAppearance = barApp;
            self.navigationController?.navigationBar.standardAppearance = barApp;
        }else{
            // 2、设置导航栏背景图片
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
            // 3、设置导航栏阴影图片
            self.navigationController?.navigationBar.shadowImage = UIImage()
        }
        //透明
        self.navigationController?.navigationBar.isTranslucent = true
        //navigation控件颜色
        self.navigationController?.navigationBar.tintColor = UIColor.white;
    }
    override var preferredStatusBarStyle: UIStatusBarStyle{
        if statusBarStyleLight {
            return .lightContent
        } else {
            if #available(iOS 13.0, *){
                return .darkContent
            }
            return .default
        }
    }
    
}
